package com.zhj.rabbitmq.demo.delayqueue;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengjibin
 * @version 1.0.0
 * @ClassName DelayedExchangeConfig.java
 * @Description 延迟交换机  通过插件实现延迟交换机
 * @createTime 2023年03月23日 21:14:00
 */
@Configuration
public class DelayedExchangeConfig {

    public static final String DELAYED_EXCHANGE = "delayed_exchange";

    public static final String DELAYED_QUEUE = "delayed_queue";

    public static final String DELAYED_RK = "delayed_rk";//routingkey

    private static final String EXCHAGE_TYPE = "x-delayed-message";//交换机类型
    /**
     * 自定义交换机
     */
    @Bean(DELAYED_EXCHANGE)
    public CustomExchange delayedExchage(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");//交换机类型
        /**
         * 1.交换机名称
         * 2.交换机类型
         * 3.是否需要持久化
         * 4.是否需要自动删除
         * 5.其他参数
         */
        return new CustomExchange(DELAYED_EXCHANGE,EXCHAGE_TYPE,true,false,arguments);
    }

    @Bean(DELAYED_QUEUE)
    public Queue createQueue(){

        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    @Bean
    public Binding bindingDelayed(@Qualifier(DELAYED_QUEUE) Queue queue,@Qualifier(DELAYED_EXCHANGE) CustomExchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(DELAYED_RK).noargs();
    }
}
